package com.abc.service;

import com.abc.bean.Purchase;
import com.abc.repository.PurchaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-07-23
 */
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements PurchaseService {

    @Autowired
    PurchaseMapper purchaseMapper;

    @Transactional
    @Override
    public boolean addPurchase(Purchase purchase) {
        Purchase p = purchaseMapper.findByName(purchase.getName());
        if (p == null) {
            purchaseMapper.insert(purchase);
            return true;
        } else {
            p.setCount(p.getCount() + purchase.getCount());
            purchaseMapper.updateById(p);
        }
        return true;
    }

    @Override
    public Purchase getPurchaseByName(String name) {
        return purchaseMapper.findByName(name);
    }
}
